package com.cplexValue;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class Simplex_Method_Data {
    public static double[] c;  // 目标函数中变量xj的价值系数cj
    public static double[] pi; // 检验系数π
    public static double[] theta; // 入基时对应的θ
    public static double[] b0; // 基本可行解中基变量的值
    public static int m; // 秩
    public static int n; // 变量个数
    public static int[] basicVar; //用来存储基变量的索引
    public static double var;
    public static int[] MData;

    /**
     * 用于初始化A
     */
    public static double[][] centreMatrix_Creat(data data){                            //建立约束矩阵
        ArrayList<int[]> goal=data.getGoalJ();
        ArrayList<int[]> setO=data.getSetO();
        m = data.getAllJ().size()+goal.size();                                       //约束矩阵的行数
        n = data.getSetO().size()+goal.size()+m;
        b0=new double[m];
        double[][] A = new double[m][n];
        for (int i = 0; i <  m ; i++) {
            for (int j = 0; j < n; j++) {
                A[i][j]=0;
                if ( i < data.getAllJ().size() && j < data.getSetO().size()){
                    int[] list= setO.get(j);//获取对应切割方式
                    A[i][j]=(-1)*aJudge( data.getAllJ() , list[0] , list[1] , list[2] , i);
                    if (list[2] == i) {
                        A[i][j] += 1;
                    }//确定与x变量相关联的矩阵部分
                }
                if (i < data.getAllJ().size() && j >= data.getSetO().size() && j < data.getSetO().size()+goal.size()){
                    for (int index=0;index<goal.size();index++) {
                        int[] into =goal.get(index);
                        if (into[0] == i) {
                            A[i][index+data.getSetO().size()-1] = 1;
                            break;
                        }
                    }
                }//确定与x变量相关联的yj部分
                if (i==data.getAllJ().size() && j<data.getSetO().size()){
                    int[] list= setO.get(j);
                    if (list[2]== 0){
                        A[i][j]=1;
                    }
                }//y0
                if (i>data.getAllJ().size() && j >= data.getSetO().size() ){
                    for (int index=0;index<goal.size();index++) {
                        int[] into =goal.get(index);
                        if (into[0] == i-data.getAllJ().size()) {
                            A[i][index+data.getSetO().size()-1] = 1;
                            break;
                        }
                    }
                }//uj
            }
        }
        for (int j = 0; j < m; j++) {
            A[j][n-m+j]=1;
        }
//        printMatrix(A);
        return A;
    }

    private static double aJudge(ArrayList<int[]> allJ,int o,int q,int k,int j){
        int[] k_set = allJ.get(k);
        int[] j_set = allJ.get(j);
        if (o == 1) {
            if (j_set[1] == k_set[1] && q == j_set[0] && (k_set[0] - q) == j_set[0]) return 2.0;
            if (j_set[1] == k_set[1] && (q == j_set[0] || (k_set[0] - q) == j_set[0])) return 1.0;
        }else {
            if (k_set[0] == j_set[0] && q == j_set[1] && (k_set[1] - q) == j_set[1]) return 2.0;
            if (k_set[0] == j_set[0] && (q == j_set[1] || (k_set[1] - q) == j_set[1])) return 1.0;
        }
        return 0.0;
    }

    /**
     * 打印单纯性表
     */
    public static void printMatrix(double[][] E){
        DecimalFormat sc = new DecimalFormat("0.00");
        for (int i = 0; i < m; i++) {
            System.out.print(sc.format(b0[i])+",");
            for (int j = 0; j < n; j++) {
                System.out.print(sc.format(E[i][j])+(j==n-1?"":","));
            }
            System.out.println();
        }
        System.out.print(var+",");
//        for (int j = 0; j < n; j++) {
//            System.out.print(sc.format(pi[j])+(j==n-1?"":","));
//        }
        System.out.println();
    }
    //示例代码段
//    public static double[][] centreMatrix_Creat(data data){                            //建立约束矩阵
//        m=3;
//        n=5;
//        b0=new double[m];
//        double[][] A = new double[m][n];
//        A[0][0]=1;A[0][1]=1;A[0][2]=1;
//        A[1][0]=2;A[1][1]=1;A[1][3]=1;
//        A[2][1]=1;A[2][4]=1;
//        return A;
//    }

}